package com.rs.game.player;

import java.io.Serializable;

import com.rs.cache.loaders.ItemDefinitions;
import com.rs.game.item.Item;
import com.rs.game.item.ItemsContainer;

public final class Equipment implements Serializable {

	private static final long serialVersionUID = -4147163237095647617L;
	
	public static final byte SLOT_HAT = 0, SLOT_CAPE = 1, SLOT_AMULET = 2,
	SLOT_WEAPON = 3, SLOT_CHEST = 4, SLOT_SHIELD = 5, SLOT_LEGS = 7,
	SLOT_HANDS = 9, SLOT_FEET = 10, SLOT_RING = 12, SLOT_ARROWS = 13;
	
	private static String[] CAPES = { "druidic cloak", "cape", "Cape",
	"Ava's accumulator" };
	private static String[] HATS = { "druidic wreath", "halo", "Royal",
	"crown", "sallet", "helm", "hood", "coif", "Coif", "partyhat",
	"Hat", "full helm (t)", "full helm (g)", "hat (t)", "hat (g)",
	"cav", "boater", "helmet", "mask", "Helm of neitiznot" };
	private static String[] BOOTS = { "boots", "Boots", "shoes", "Shoes" };
	private static String[] GLOVES = { "gloves", "gauntlets", "Gloves",
	"vambraces", "vamb", "bracers" };
	private static String[] SHIELDS = { "kiteshield", "sq shield", "Toktz-ket",
	"books", "book", "kiteshield (t)", "kiteshield (g)",
	"kiteshield(h)", "defender", "shield" };
	private static String[] AMULETS = { "amulet", "necklace", "Amulet of",
	"scarf" };
	private static String[] ARROWS = { "arrow", "arrows", "arrow(p)",
	"arrow(+)", "arrow(s)", "bolt", "Bolt rack", "Opal bolts",
	"Dragon bolts", "bolts (e)", "bolts", "Hand cannon shot" };
	private static String[] RINGS = { "ring", "Ring" };
	private static String[] BODY = { "armour", "hauberk", "platebody",
	"chainbody", "robetop", "leathertop", "platemail", "top",
	"brassard", "Robe top", "body", "platebody (t)", "platebody (g)",
	"body(g)", "body_(g)", "chestplate", "torso", "shirt" };
	private static String[] LEGS = { "druidic robe", "cuisse", "pants",
	"platelegs", "plateskirt", "skirt", "bottoms", "chaps",
	"platelegs (t)", "platelegs (g)", "bottom", "skirt", "skirt (g)",
	"skirt (t)", "chaps (g)", "chaps (t)", "tassets", "legs",
	"trousers", "robe bottom" };
	private static String[] WEAPONS = {"rapier", "hatchet", "bow", "Hand cannon",
	"Inferno adze", "Silverlight", "Darklight", "wand",
	"Statius's warhammer", "anchor", "spear.", "Vesta's longsword.",
	"scimitar", "longsword", "sword", "longbow", "shortbow", "dagger",
	"mace", "halberd", "spear", "Abyssal whip", "Abyssal vine whip", "Ornate katana", "axe", "flail",
	"crossbow", "Torags hammers", "dagger(p)","dagger (p++)", "dagger(+)",
	"dagger(s)", "spear(p)", "spear(+)", "spear(s)", "spear(kp)",
	"maul", "dart", "dart(p)", "javelin", "javelin(p)", "knife",
	"knife(p)", "Longbow", "Shortbow", "Crossbow", "Toktz-xil",
	"Toktz-mej", "Tzhaar-ket", "staff", "Staff", "godsword", "c'bow",
	"Crystal bow", "Dark bow", "claws", "warhammer", "adze", "hand" };
	private static String[] FULL_BODY = { "armour", "hauberk", "top", "shirt",
	"platebody", "Ahrims robetop", "Karils leathertop", "brassard",
	"Robe top", "robetop", "platebody (t)", "platebody (g)",
	"chestplate", "torso", "Morrigan's", "leather body", "chainbody",
	"Zuriel's", "robe top" };
	private static String[] FULL_HAT = { "sallet", "med helm", "coif",
	"Dharoks helm","Initiate helm", "Coif",
	"Helm of neitiznot", "tiara"};
	private static String[] FULL_MASK = { "sallet", "full helm", "mask",
	"Veracs helm", "Guthans helm", "Torags helm", "Karils coif",
	"full helm (t)", "full helm (g)", "mask" };
	
	private ItemsContainer<Item> items;
	
	private transient Player player;
	private transient int equipmentHpIncrease;
	
	public Equipment() {
		items = new ItemsContainer<Item>(15, false);
	}

	public void setPlayer(Player player) {
		this.player = player;
	}
	
	public void init() {
		player.getPackets().sendItems(387, 29, 94, items);
		refresh(null);
	}
	
	public void refresh(int... slots) {
		if(slots != null) {
			player.getPackets().sendUpdateItems(387, 29, 94, items, slots);
			player.getCombatDefinitions().checkAttackStyle();
		}
		player.getCombatDefinitions().refreshBonuses();
		refreshEquipmentHp(slots == null);
	}
	
	public void reset() {
		items.reset();
		init();
	}
	
	public Item getItem(int slot) {
		return items.get(slot);
	}
	
	public void refreshEquipmentHp(boolean init) {
		double hpIncrease = 0;
		for(int index = 0; index < items.getSize(); index++) {
			Item item = items.get(index);
			if (item == null)
				continue;
			int id = item.getId();
			if(index == Equipment.SLOT_HAT) {
			if(id == 20135 || id == 20137 //torva
					|| id == 20147 || id == 20149 //pernix
					|| id == 20159 || id == 20161 //virtus
					)
				hpIncrease += 66;
			}else if (index == Equipment.SLOT_CHEST) {
				if(id == 20139 || id == 20141 //torva
						|| id == 20151 || id == 20153 //pernix
						|| id == 20163 || id == 20165 //virtus
						)
					hpIncrease += 200;	
			}else if (index == Equipment.SLOT_LEGS) {
				if(id == 20143 || id == 20145 //torva
						|| id == 20155 || id == 20157 //pernix
						|| id == 20167 || id == 20169 //virtus
						)
					hpIncrease += 134;	
			}
			
		}
		if (hpIncrease != equipmentHpIncrease) {
			equipmentHpIncrease = (int) hpIncrease;
			if(!init)
				player.refreshHitPoints();
		}
	}
	
	public static boolean isFullBody(Item item) {
		String itemName = item.getDefinitions().getName();
		if(itemName == null)
			return false;
		for (int i = 0; i < FULL_BODY.length; i++)
			if (itemName.contains(FULL_BODY[i]))
				return true;
		return false;
	}
	
	public static boolean isFullHat(Item item) {
		String itemName = item.getDefinitions().getName();
		if(itemName == null)
			return false;
		for (int i = 0; i < FULL_HAT.length; i++) {
			if (itemName.contains(FULL_HAT[i])) {
				return true;
			}
		}
		return false;
	}
	
	public static boolean isFullMask(Item item) {
		String itemName = item.getDefinitions().getName();
		if(itemName == null)
			return false;
		for (int i = 0; i < FULL_MASK.length; i++)
			if (itemName.contains(FULL_MASK[i]))
				return true;
		return false;
	}
	
	public static int getItemSlot(int itemId) {
		String item = ItemDefinitions.getItemDefinitions(itemId).getName();
		if(item == null)
			return -1;
		for (int i = 0; i < CAPES.length; i++)
			if (item.contains(CAPES[i]))
				return 1;
		for (int i = 0; i < HATS.length; i++)
			if (item.contains(HATS[i]))
				return 0;
		for (int i = 0; i < BOOTS.length; i++)
			if (item.endsWith(BOOTS[i]) || item.startsWith(BOOTS[i]))
				return 10;
		for (int i = 0; i < GLOVES.length; i++)
			if (item.endsWith(GLOVES[i]) || item.startsWith(GLOVES[i]))
				return 9;
		for (int i = 0; i < SHIELDS.length; i++)
			if (item.contains(SHIELDS[i]))
				return 5;
		for (int i = 0; i < AMULETS.length; i++)
			if (item.endsWith(AMULETS[i]) || item.startsWith(AMULETS[i]))
				return 2;
		for (int i = 0; i < ARROWS.length; i++)
			if (item.endsWith(ARROWS[i]) || item.startsWith(ARROWS[i]))
				return 13;
		for (int i = 0; i < RINGS.length; i++)
			if (item.endsWith(RINGS[i]) || item.startsWith(RINGS[i]))
				return 12;
		for (int i = 0; i < BODY.length; i++)
			if (item.contains(BODY[i]))
				return 4;
		for (int i = 0; i < LEGS.length; i++)
			if (item.contains(LEGS[i]))
				return 7;
		for (int i = 0; i < WEAPONS.length; i++)
			if (item.endsWith(WEAPONS[i]) || item.startsWith(WEAPONS[i]))
				return 3;
		return -1;
}
	
	public boolean hasTwoHandedWeapon() {
		Item item = items.get(SLOT_WEAPON);
		if(item == null)
			return false;
		return isTwoHandedWeapon(item);
	}
	public static boolean isTwoHandedWeapon(Item item) {
		int itemId = item.getId();
		if (itemId == 4212)
			return true;
		else if (itemId == 4214)
			return true;
		String wepEquiped = item.getDefinitions().getName();
		if(wepEquiped == null)
			return false;
		else if (wepEquiped.endsWith("claws"))
			return true;
		else if (wepEquiped.endsWith("anchor"))
			return true;
		else if (wepEquiped.endsWith("2h sword"))
			return true;
		else if (wepEquiped.endsWith("longbow"))
			return true;
		else if (wepEquiped.equals("Seercull"))
			return true;
		else if (wepEquiped.endsWith("shortbow"))
			return true;
		else if (wepEquiped.endsWith("Longbow"))
			return true;
		else if (wepEquiped.endsWith("Shortbow"))
			return true;
		else if (wepEquiped.endsWith("bow full"))
			return true;
		else if (wepEquiped.equals("Dark bow"))
			return true;
		else if (wepEquiped.endsWith("halberd"))
			return true;
		else if (wepEquiped.equals("Granite maul"))
			return true;
		else if (wepEquiped.equals("Karils crossbow"))
			return true;
		else if (wepEquiped.equals("Torags hammers"))
			return true;
		else if (wepEquiped.equals("Veracs flail"))
			return true;
		else if (wepEquiped.equals("Dharoks greataxe"))
			return true;
		else if (wepEquiped.equals("Guthans warspear"))
			return true;
		else if (wepEquiped.equals("Tzhaar-ket-om"))
			return true;
		else if (wepEquiped.endsWith("godsword"))
			return true;
		else if (wepEquiped.equals("Saradomin sword"))
			return true;
		return false;
	}

	public boolean usingSword(Item item) {
		String wepEquiped = item.getDefinitions().getName();
		if(wepEquiped.contains(" sword")) {
			return true;
		}	
		return false;
	}

	public boolean usingScimitar(Item item) {
		String wepEquiped = item.getDefinitions().getName();
		if(wepEquiped.contains(" scimitar")) {
			return true;
		}	
		return false;
	}
	public boolean usingDarts(Item item) {
		String wepEquiped = item.getDefinitions().getName();
		if(wepEquiped.contains("darts") || wepEquiped.contains("kni")) {
			return true;
		}	
		return false;
	}
	public boolean usingDagger(Item item) {
		String wepEquiped = item.getDefinitions().getName();
		if(wepEquiped.contains("dagger")) {
			return true;
		}	
		return false;
	}
	public boolean usingBattleaxe(Item item) {
		String wepEquiped = item.getDefinitions().getName();
		if(wepEquiped.contains("battleaxe")) {
			return true;
		}	
		return false;
	}
	public boolean usingBow(Item item) {
		String wepEquiped = item.getDefinitions().getName();
		if(!(wepEquiped.contains("crossbow")) && wepEquiped.contains("bow")) {
			return true;
		}	
		return false;
	}
	
	public int getWeaponRenderEmote() {
		Item weapon = items.get(3);
		if (weapon == null)
			return 1426;
		return weapon.getDefinitions().getRenderAnimId();
	}
	
	public boolean hasShield() {
		return items.get(5) != null;
	}
	
	public int getWeaponId() {
		Item item = items.get(SLOT_WEAPON);
		if(item == null)
			return -1;
		return item.getId();
	}
	
	public int getChestId() {
		Item item = items.get(SLOT_CHEST);
		if(item == null)
			return -1;
		return item.getId();
	}
	
	public int getHatId() {
		Item item = items.get(SLOT_HAT);
		if(item == null)
			return -1;
		return item.getId();
	}
	public int getShieldId() {
		Item item = items.get(SLOT_SHIELD);
		if(item == null)
			return -1;
		return item.getId();
	}
	
	
	public int getLegsId() {
		Item item = items.get(SLOT_LEGS);
		if(item == null)
			return -1;
		return item.getId();
	}
	
	public void removeAmmo(int ammoId, int ammount) {
		if(ammount == -1) {
			items.remove(SLOT_WEAPON, new Item(ammoId, 1));
			refresh(SLOT_WEAPON);
		}else{
			items.remove(SLOT_ARROWS, new Item(ammoId, ammount));
			refresh(SLOT_ARROWS);
		}
	}
	
	public int getCapeId() {
		Item item = items.get(SLOT_CAPE);
		if(item == null)
			return -1;
		return item.getId();
	}
	
	public int getAmmoId() {
		Item item = items.get(SLOT_ARROWS);
		if(item == null)
			return -1;
		return item.getId();
	}
	
	public int getBootsId() {
		Item item = items.get(SLOT_FEET);
		if(item == null)
			return -1;
		return item.getId();
	}
	
	public ItemsContainer<Item> getItems() {
		return items;
	}
	
	public int getEquipmentHpIncrease() {
		return equipmentHpIncrease;
	}
	
}
